package com.heima.user.service.impl;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.LoginDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.service.ApUserService;
import com.heima.utils.common.AppJwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Service
@Transactional
@Slf4j
public class ApUserServiceImpl extends ServiceImpl<ApUserMapper, ApUser> implements ApUserService {
    @Autowired
    private ApUserMapper apUserMapper;

    /**
     * app端登录
     *
     * @param loginDto
     * @return
     */
    @Override
    public ResponseResult login(LoginDto loginDto) {

        if (StringUtils.isNotBlank(loginDto.getPhone()) && StringUtils.isNotBlank(loginDto.getPassword())) {
            //登录
            //查找手机号
            ApUser apUser = this.getOne(Wrappers.<ApUser>lambdaQuery().eq(ApUser::getPhone, loginDto.getPhone()));

            if (apUser == null) {
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "用户不存在");
            }
            //校验密码
            String salt = apUser.getSalt();
            String pwd = loginDto.getPassword();

            pwd = DigestUtils.md5DigestAsHex((pwd + salt).getBytes());

            if (!StringUtils.equals(pwd, apUser.getPassword())) {
                return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
            }

            //返回token
            Map<String, Object> map = new HashMap<>();
            map.put("token", AppJwtUtil.getToken(apUser.getId().longValue()));
            apUser.setSalt("");
            apUser.setPassword("");
            map.put("user", apUser);
            return ResponseResult.okResult(map);
        } else {
            //不登陆
            Map<String, Object> map = new HashMap<>();
            map.put("token", AppJwtUtil.getToken(0L));
            return ResponseResult.okResult(map);
        }
    }

    /**
     * 注册用户
     *
     * @param apUser
     * @return
     */
    @Override
    public ResponseResult register(ApUser apUser) {
        //如果 手机号 密码 任一为空 错
        if (StringUtils.isBlank(apUser.getPhone()) || StringUtils.isBlank(apUser.getPassword())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.AP_USER_DATA_NOT_EXIST);
        }

        //如果手机号存在
        ApUser apUser1 = this.getOne(Wrappers.<ApUser>lambdaQuery().eq(ApUser::getPhone, apUser.getPhone()));
        if (apUser1!=null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"手机号已注册");
        }


        //生成 salt
        String salt = UUID.randomUUID().toString().replace("-", "");
        //生成密码
        String password = DigestUtils.md5DigestAsHex((apUser.getPassword() + salt).getBytes());

        apUser.setSalt(salt);
        apUser.setPassword(password);
        apUser.setCreatedTime(new Date());

        //保存用户
        boolean result = this.save(apUser);

        if (!result){
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }

        return ResponseResult.okResult(null);
    }

}
